package com.ruiwen.imuser.service.impl;

import com.ruiwen.imuser.model.entity.UserEntity;
import com.ruiwen.imuser.service.UserService;
import com.ruiwen.imuser.mapper.UserMapper;
import com.ruiwen.im.common.response.IMCommonResponse;
import com.ruiwen.im.common.response.IMCommonResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public IMCommonResponse<UserEntity> getById(Integer id) {

        IMCommonResponse<UserEntity> imCommonResponse = new IMCommonResponse<>(IMCommonResultCode.FAIL);
        try {
            UserEntity userEntity = userMapper.getById(id);
            imCommonResponse.setIMCommonResultCode(IMCommonResultCode.SUCCESS);
            imCommonResponse.setData(userEntity);

            return imCommonResponse;
        } catch (Exception e) {
            log.error("用户注册失败{}",e.getMessage());
            return imCommonResponse;
        }
    }

    @Override
    public IMCommonResponse<String> addUser(UserEntity userEntity) {
        IMCommonResponse<String> imCommonResponse = new IMCommonResponse<>(IMCommonResultCode.FAIL);
        try {
            userMapper.addUser(userEntity);
            imCommonResponse.setIMCommonResultCode(IMCommonResultCode.SUCCESS);
        } catch (Exception e) {
            log.error("用户注册失败{}",e.getMessage());
            imCommonResponse.setMessage(e.getMessage());
        }
        return imCommonResponse;
    }

    @Override
    public IMCommonResponse<Object> updateById(UserEntity userEntity) {

        IMCommonResponse<Object> imCommonResponse = new IMCommonResponse<>(IMCommonResultCode.FAIL);
        try {
            int updateLows=userMapper.updateById(userEntity);
            if (updateLows==0) {
                return imCommonResponse;
            }
            imCommonResponse.setIMCommonResultCode(IMCommonResultCode.SUCCESS);
            return imCommonResponse;
        } catch (Exception e) {
            log.error("用户更新失败");
            imCommonResponse.setMessage(e.getMessage());
            return imCommonResponse;
        }

    }
}
